package ticketManagement.servlet;

import java.io.IOException;
import java.security.InvalidParameterException;
import java.text.ParseException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import accountManagement.Client;
import ticketManagement.SeasonTicket;
import ticketManagement.Ticket;
import ticketManagement.TicketOffice;
import transportManagement.TransportBoard;
import transportManagement.Transport;
import common.Date;

/**
 * Servlet implementation class ServletAcquistaBiglietto
 */
@WebServlet("/ServletBuyTicket")
public class ServletBuyTicket extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletBuyTicket() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doService(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doService(request, response);
	}

    private void doService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	try {
    		Object user = request.getSession().getAttribute("user");
        	if(user == null || !(user instanceof Client)) {
        		throw new InvalidParameterException("Non disponi dei privilegi necessari per accedere alla pagina richiesta");
        	} 
    		String type = request.getParameter("type");
    		String transportId = request.getParameter("transportId");
    		
			Transport transport = new Transport();
			transport.setTransportId(transportId);
			List<Transport> result = TransportBoard.getInstance().search(transport);
		
			if(result.size() != 1) {
				throw new InvalidParameterException("Transport not found");
			}
			transport = result.get(0);
			
			Ticket ticket; 
			switch (type) {
				case "seasonTicket":
					Date dataInizio = Date.parseDate(request.getParameter("beginDate"));
	    			Date dataFine = Date.parseDate(request.getParameter("endDate"));
	    			ticket = new SeasonTicket(transport, dataInizio, dataFine);
					break;
				default:
					ticket = new Ticket(transport);
			}
			TicketOffice.getInstance().add(ticket);
			
			request.setAttribute("resultTitle", "Acquisto biglietto");
			request.setAttribute("resultContent", "Acquisto riuscito: " + ticket);
			request.getRequestDispatcher("/result.jsp").forward(request, response);
		} catch(InvalidParameterException | NullPointerException | ParseException e) {
			request.setAttribute("error", "Formato della data non valido");
			request.getRequestDispatcher("/errorPage.jsp").forward(request, response);
		}
    }
}
